﻿using System;
using System.Collections.Generic;
using Spaanjaars.ContactManager.Infrastructure.Validation;

namespace Spaanjaars.ContactManager.Infrastructure.Domain
{
    public abstract class Entity<TId, T> : DomainObject<T>
    {                
        public TId Id { get; set; }
            
        public override bool Equals(object entity)
        {
            return entity != null
               && entity is Entity<TId, T>
               && this == (Entity<TId, T>)entity;
        }

        public override int GetHashCode()
        {
            return this.Id.GetHashCode();
        }

        public static bool operator ==(Entity<TId, T> entity1, Entity<TId, T> entity2)
        {
            if ((object)entity1 == null && (object)entity2 == null)
            {
                return true;
            }

            if ((object)entity1 == null || (object)entity2 == null)
            {
                return false;
            }

            if (entity1.Id.ToString() == entity2.Id.ToString())
            {
                return true;
            }

            return false;
        }

        public static bool operator !=(Entity<TId, T> entity1,
            Entity<TId, T> entity2)
        {
            return (!(entity1 == entity2));
        }        
    }
}
